﻿//===================================================================================
// Copyright (c) Ryan Garrett.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
using System;

namespace Caffeine.Core.ModelProperty
{
    internal class ComputedModelPropertySync<RegisteredPropertyType, PropertyType> : IPropertySync<RegisteredPropertyType>
    {
        private readonly ComputedModelProperty<RegisteredPropertyType, PropertyType> _parentProperty;

        public ComputedModelPropertySync(ComputedModelProperty<RegisteredPropertyType, PropertyType> parentProperty)
        {
            //NOTE: It's ok to have a hard reference to the property since registered properties will always exist in the same model
            //as the computed property
            _parentProperty = parentProperty;
        }

        public ModelScalarPropertyBase ParentProperty
        {
            get
            {
                return _parentProperty;
            }
        }

        public ModelProperty<RegisteredPropertyType> SyncedToProperty
        {
            get
            {
                return null;
            }
        }

        public WeakReference SyncedToPropertyReference
        {
            get
            {
                return null;
            }
        }

        public virtual void SignalValueChange(RegisteredPropertyType value)
        {
            Sync(value);
        }

        public virtual void SignalCommit(RegisteredPropertyType committedValue)
        {
            Sync(committedValue);
        }

        private void Sync(RegisteredPropertyType value)
        {
            (ParentProperty as ComputedModelProperty<RegisteredPropertyType, PropertyType>).SyncValue(value);
        }
    }
}
